<<<<<<< HEAD
// import Alert from "../../../util/alent.js";
=======
import Alert from "../../../util/alent.js";
>>>>>>> 758185efc9ae7ad1fbf8d5ab9198f268705acd49
import { obj } from "../../index.js";
import dom, { ajax } from "../../init.js";
import { islogin } from "./user.js";

export default {
  mubuscroll(e: Event) {
    e.preventDefault();
  },
  mubucheblock(e: Event) {
    if (dom.user.mubu.style.display == "none") {
      dom.user.mubu.style.display = "block";
    } else {
      if (e.target === dom.user.mubu) dom.user.mubu.style.display = "none";
    }
  },
  cheLoginRegistry(e: Event) {
    if (e.target === dom.user.mubuevent.tologin) {
      dom.user.mubuevent.toregistry.classList.remove("cheele");
      dom.user.mubuevent.tologin.classList.add("cheele");
      dom.user.mubuevent.nickNamemodule.style.display = "none";
      dom.user.mubuevent.namemodule.style.marginTop = "20px";
      dom.user.mubuevent.namemodule.style.borderTop = "1px solid #e7e7e7";
      dom.user.mubuevent.namemodule.style.border = "1px solid #e7e7e7";
      dom.user.mubuevent.namemodule.style.borderRadius = "8px 8px 0 0";
      dom.user.mubuevent.registrybtn.style.display = "none";
      dom.user.mubuevent.ts.style.paddingTop = "10px";
      dom.user.mubuevent.loginbtn.style.display = "block";
      dom.user.mubuevent.ipt.name.setAttribute("placeholder", "输入用户名");
      dom.user.mubuevent.ipt.pwd.setAttribute("placeholder", "输入密码");
    }
    if (e.target === dom.user.mubuevent.toregistry) {
      dom.user.mubuevent.toregistry.classList.add("cheele");
      dom.user.mubuevent.tologin.classList.remove("cheele");
      dom.user.mubuevent.nickNamemodule.style.display = "flex";
      dom.user.mubuevent.nickNamemodule.style.borderBottom =
        "1px solid #e7e7e7";
      dom.user.mubuevent.namemodule.style.marginTop = "0";
      dom.user.mubuevent.ts.style.paddingTop = "0px";
      dom.user.mubuevent.namemodule.style.borderRadius = "0";
      dom.user.mubuevent.namemodule.style.borderTop = "0";
      dom.user.mubuevent.registrybtn.style.display = "block";
      dom.user.mubuevent.loginbtn.style.display = "none";
      dom.user.mubuevent.ipt.name.setAttribute(
        "placeholder",
        "必须由字母开头,由数字字母组成3-9位"
      );
      dom.user.mubuevent.ipt.pwd.setAttribute(
        "placeholder",
        "6-16位大小写字母下划线组成"
      );
    }
  },

  //优化获取焦点
  nicknamefocus() {
    dom.user.mubuevent.ipt.nickname.focus();
  },
  namefocus() {
    dom.user.mubuevent.ipt.name.focus();
  },
  pwdfocus() {
    dom.user.mubuevent.ipt.pwd.focus();
  },

  //重置
  chongzhi() {
    if (dom.user.mubuevent.tologin.classList.length <= 1) {
      dom.user.mubuevent.namemodule.style.border = "1px solid #e7e7e7";
      dom.user.mubuevent.nickNamemodule.style.borderBottom =
        "1px solid #e7e7e7";
      dom.user.mubuevent.namemodule.style.borderTop = "0";
    }
    (dom.user.mubuevent.ipt.name as any).value = "";
    (dom.user.mubuevent.ipt.pwd as any).value = "";
    (dom.user.mubuevent.ipt.nickname as any).value = "";
  },

  //用户名验证
  async isname() {
    if (dom.user.mubuevent.toregistry.classList.length >= 2) {
      const name = /^[A-Za-z][A-Za-z0-9]{2,9}$/;
      if (name.test((dom.user.mubuevent.ipt.name as any).value.trim())) {
        const is = await ajax.get("/user/isname", {
          name: (dom.user.mubuevent.ipt.name as any).value.trim(),
        });

        if (JSON.parse(is as string).msg === "用户名可以使用") {
          dom.user.mubuevent.namemodule.style.border = "1px solid #0f0";
          dom.user.mubuevent.namemodule.style.borderTop = "0";
          dom.user.mubuevent.nickNamemodule.style.borderBottom =
            "1px solid #0f0";
          dom.user.mubuevent.ts.style.color = "#0f0";
          dom.user.mubuevent.ts.innerText = JSON.parse(is as string).msg;
        } else {
          dom.user.mubuevent.namemodule.style.border = "1px solid #f00";
          dom.user.mubuevent.namemodule.style.borderTop = "0";
          dom.user.mubuevent.nickNamemodule.style.borderBottom =
            "1px solid #f00";
          dom.user.mubuevent.ts.style.color = "#f00";
          dom.user.mubuevent.ts.innerText = "用户名重复";
        }
      } else {
        dom.user.mubuevent.namemodule.style.border = "1px solid #f00";
        dom.user.mubuevent.namemodule.style.borderTop = "0";
        dom.user.mubuevent.nickNamemodule.style.borderBottom = "1px solid #f00";
        dom.user.mubuevent.ts.style.color = "#f00";
        dom.user.mubuevent.ts.innerText = "用户名不符合规范";
      }
      let id: any;
      clearTimeout(id);
      id = setTimeout(() => {
        dom.user.mubuevent.ts.style.color = "#fff";
        dom.user.mubuevent.ts.innerText = "";
      }, 2000);
    }
  },
};

//登录
export async function login() {
  const data = await ajax.post("/user/login", {
    name: (dom.user.mubuevent.ipt.name as any).value.trim(),
    pwd: (dom.user.mubuevent.ipt.pwd as any).value.trim(),
  });
  if (JSON.parse(data as string).msg === "登录成功") {
    dom.user.mubuevent.ts.style.color = "#0f0";
    dom.user.mubuevent.ts.innerText = JSON.parse(data as string).msg;
    setTimeout(() => {
      dom.user.mubuevent.ts.style.color = "#fff";
      dom.user.mubuevent.ts.innerText = "";
      dom.user.mubu.style.display = "none";
      islogin();
    }, 1000);
  } else {
<<<<<<< HEAD
    alert(JSON.parse(data as string).msg);
=======
    new Alert(JSON.parse(data as string).msg);
>>>>>>> 758185efc9ae7ad1fbf8d5ab9198f268705acd49
  }
}

//注册
export async function registry() {
  if (
<<<<<<< HEAD
    (dom.user.mubuevent.ipt.nickname as any).value.trim().length < 2 ||
    (dom.user.mubuevent.ipt.nickname as any).value.trim().length > 9
  ) {
    return alert("昵称不符合规范");
=======
    (dom.user.mubuevent.ipt.nickname as any).value.trim().length < 3 ||
    (dom.user.mubuevent.ipt.nickname as any).value.trim().length > 9
  ) {
    return new Alert("昵称不符合规范");
>>>>>>> 758185efc9ae7ad1fbf8d5ab9198f268705acd49
  }
  const data = await ajax.post("/user/registry", {
    name: (dom.user.mubuevent.ipt.name as any).value.trim(),
    pwd: (dom.user.mubuevent.ipt.pwd as any).value.trim(),
    nickName: (dom.user.mubuevent.ipt.nickname as any).value.trim(),
  });
  const result: any = JSON.parse(data as string);
  if (result.msg === "注册成功") {
    dom.user.mubuevent.ts.style.color = "#0f0";
    dom.user.mubuevent.ts.innerText = JSON.parse(data as string).msg;
    setTimeout(() => {
      dom.user.mubuevent.ts.style.color = "#fff";
      dom.user.mubuevent.ts.innerText = "";
      dom.user.mubu.style.display = "none";
      islogin();
    }, 1000);
  } else {
    alert(JSON.parse(data as string).msg);
  }
}
